from django.db import models

# Create your models here.
from utils.common_basemodel import BaseModels
from user.models import Department, Doctor


# 门诊住院信息表
class PatientInfo(BaseModels):
    type = (
        (0, '暂未查询'),
        (1, 'O型'),
        (2, 'A型'),
        (3, 'B型'),
        (4, 'AB型'),
    )
    id_care = models.CharField(max_length=32, verbose_name='病人身份证')
    blood_type = models.SmallIntegerField(choices=type, default=0, verbose_name='血型')
    # hospital_time = models.DateTimeField(verbose_name='住院时间')
    family_mobile = models.CharField(max_length=32, verbose_name='家属联系电话')
    # 外键关联病人结果表
    doctor = models.ForeignKey(to=Doctor, on_delete=models.CASCADE)
    # 属于哪个科室
    department = models.ForeignKey(to=Department, on_delete=models.SET_NULL, null=True)
    # 一个病人一个诊断结果， 根据该一对一判断该用户是否诊断
    result = models.OneToOneField(to='Result', on_delete=models.CASCADE)

    # 属于哪个病房
    # ward = models.ForeignKey(to='Ward', on_delete=models.CASCADE)

    class Meta:
        verbose_name = '门诊住院信息表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    def blood(self):
        return self.get_blood_type_display()


class Result(models.Model):
    result_content = models.TextField(verbose_name='诊断内容')
    result_time = models.DateTimeField(auto_now=True, verbose_name='诊断结果时间')

    class Meta:
        verbose_name = '住院病人诊断结果表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.result_content


class Home(models.Model):
    type = (
        (1, '智齿重症室'),
        (2, '儿童观察室'),
        (3, '老人口腔种植室'),
        (4, 'VIP病人专用室')
    )
    ward_code = models.CharField(max_length=32, verbose_name='病房房间号', null=True)
    ward_number = models.IntegerField(verbose_name='房位数量')
    ward_type = models.SmallIntegerField(choices=type, default=1, verbose_name='病房类型')
    doctor = models.ForeignKey(to=Doctor, on_delete=models.SET_NULL, db_constraint=False, null=True)

    class Meta:
        verbose_name = '住院病房信息表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.get_ward_type_display()


class HospitalInfo(BaseModels):
    patient_id = models.SmallIntegerField(verbose_name='病历编号')
    blood = models.CharField(max_length=32, verbose_name='血型')
    department_name = models.CharField(max_length=32, verbose_name='科室名称')
    result = models.TextField(verbose_name='诊断结果')
    doctor_name = models.CharField(max_length=32, verbose_name='医生姓名')
    hospital_time = models.DateField(auto_now_add=True, verbose_name='入院时间')
    time_length = models.CharField(max_length=32, verbose_name='住院时长')
    home = models.ForeignKey(to='Home', related_name='wardOfPatient', on_delete=models.CASCADE)

    class Meta:
        verbose_name = '住院信息表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
